﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using DiamondDash.Views;
using DiamondDash.Entities;
using DiamondDash.Core;
using System.Windows.Threading;
using OpenXLive;
using Microsoft.Phone.Tasks;

namespace DiamondDash
{
    public partial class MainPage : PhoneApplicationPage
    {
        // 构造函数
        public MainPage()
        {
            InitializeComponent();
            isOnGift = false;
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            //清除页面堆栈
            try
            {
                for (int i = 0; i < 4; i++)
                {
                    NavigationService.RemoveBackEntry();
                }
            }
            catch
            { }

            if (StageManager.Instance.IsDoNewUserTask)
            {
                this.btnGift.Visibility = System.Windows.Visibility.Collapsed;
            }
            else
            {
                this.btnGift.Visibility = System.Windows.Visibility.Visible;
            }

            if (StageManager.Instance.IsPlaySound)
            {
                this.btnSoundOn.Visibility = System.Windows.Visibility.Visible;
                this.btnSoundOff.Visibility = System.Windows.Visibility.Collapsed;
            }
            else
            {
                this.btnSoundOn.Visibility = System.Windows.Visibility.Collapsed;
                this.btnSoundOff.Visibility = System.Windows.Visibility.Visible;
            }

            //读取当前等级和状态
            this.levelProgress.SetLevelProgress(StageManager.Instance.CurrLevel
                , StageManager.Instance.GetLevelExpPercent());
        }

        private void btnNewGame_Click(object sender, System.Windows.RoutedEventArgs e)
        {

            StageManager.Instance.LaunchCount += 1;
            //跳转到准备游戏界面
            SoundManager.PlayEffect(SoundManager.clickButton);
            this.NavigationService.Navigate(new Uri("/Pages/ReadyPage.xaml", UriKind.RelativeOrAbsolute));
        }

        private void btnRanking_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            SoundManager.PlayEffect(SoundManager.clickButton);
            //显示OpenXLive的主页
            if (XLiveGameManager.CurrentSession != null && XLiveGameManager.CurrentSession.IsValid)
            {
                OpenXLive.Silverlight.XLiveUIManager.ShowGameCenter();
            }
        }

        bool isOnGift = false;

        private void btnGift_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            SoundManager.PlayEffect(SoundManager.clickButton);
            isOnGift = true;
            this.StoryboardShowPackage.Begin();
        }


        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            if (isOnGift)
            {
                SoundManager.PlayEffect(SoundManager.clickButton);
                this.StoryboardHidePackage.Begin();
                isOnGift = false;
                e.Cancel = true;
            }
            base.OnBackKeyPress(e);
        }

        private void btnGetMB_Click_1(object sender, RoutedEventArgs e)
        {
            SoundManager.PlayEffect(SoundManager.clickButton);
            MarketplaceReviewTask marketplaceReviewTask = new MarketplaceReviewTask();
            marketplaceReviewTask.Show();
            StageManager.Instance.IsCommented = true;
            StageManager.Instance.IsDoNewUserTask = true;

            //增加10000G
            StageManager.Instance.TotalG += 10000;

            this.btnGetMB.Content = "已获得";
            this.btnGetMB.IsEnabled = false;
            this.btnGift.Visibility = System.Windows.Visibility.Collapsed;
        }

        private void btnCloseGift_Click_1(object sender, RoutedEventArgs e)
        {
            SoundManager.PlayEffect(SoundManager.clickButton);
            isOnGift = false;
            this.StoryboardHidePackage.Begin();
        }

        private void btnSoundOn_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            //关闭音效
            StageManager.Instance.IsPlaySound = false;
            this.btnSoundOn.Visibility = System.Windows.Visibility.Collapsed;
            this.btnSoundOff.Visibility = System.Windows.Visibility.Visible;
        }

        private void btnSoundOff_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            //打开音效
            StageManager.Instance.IsPlaySound = true;
            this.btnSoundOn.Visibility = System.Windows.Visibility.Visible;
            this.btnSoundOff.Visibility = System.Windows.Visibility.Collapsed;
        }

    }
}